package com.pt.dto.response;

import lombok.Data;
import java.time.LocalDateTime;

@Data
public class RequestResponse {
    private Long id;
    private String title;
    private String description;
    private Integer categoryId;
    private String categoryName;
    private Long requesterId;
    private Long userId; // 兼容前端字段名
    private String username; // 发起者用户名
    private Integer rewardPoints;
    private Integer status;
    private Long fillerId;
    private String filledBy; // 完成者用户名
    private Long filledTorrentId;
    private LocalDateTime createdTime;
    private String createdAt; // 兼容前端字段名
    private LocalDateTime filledTime;
    private String filledAt; // 兼容前端字段名
    
    // 添加种子相关字段
    private String torrentTitle;
    private Long torrentSize;
    private String torrentInfoHash;
    
    // 从Map构造RequestResponse
    public static RequestResponse fromMap(java.util.Map<String, Object> map) {
        RequestResponse response = new RequestResponse();
        response.setId(((Number) map.get("id")).longValue());
        response.setTitle((String) map.get("title"));
        response.setDescription((String) map.get("description"));
        
        if (map.get("category_id") != null) {
            response.setCategoryId(((Number) map.get("category_id")).intValue());
        }
        response.setCategoryName((String) map.get("category_name"));
        
        if (map.get("requester_id") != null) {
            Long requesterId = ((Number) map.get("requester_id")).longValue();
            response.setRequesterId(requesterId);
            response.setUserId(requesterId); // 设置兼容字段
        }
        response.setUsername((String) map.get("requester_username"));
        
        if (map.get("reward_points") != null) {
            response.setRewardPoints(((Number) map.get("reward_points")).intValue());
        }
        
        if (map.get("status") != null) {
            response.setStatus(((Number) map.get("status")).intValue());
        }
        
        if (map.get("filler_id") != null) {
            response.setFillerId(((Number) map.get("filler_id")).longValue());
        }
        response.setFilledBy((String) map.get("filler_username"));
        
        if (map.get("filled_torrent_id") != null) {
            response.setFilledTorrentId(((Number) map.get("filled_torrent_id")).longValue());
        }
        
        if (map.get("created_time") != null) {
            response.setCreatedTime((LocalDateTime) map.get("created_time"));
            response.setCreatedAt(response.getCreatedTime().toString());
        }
        
        if (map.get("filled_time") != null) {
            response.setFilledTime((LocalDateTime) map.get("filled_time"));
            response.setFilledAt(response.getFilledTime().toString());
        }
        
        // 设置种子相关信息
        response.setTorrentTitle((String) map.get("torrent_title"));
        if (map.get("torrent_size") != null) {
            response.setTorrentSize(((Number) map.get("torrent_size")).longValue());
        }
        response.setTorrentInfoHash((String) map.get("torrent_info_hash"));
        
        return response;
    }
} 